<hr/>
<div class="row-fluid">
    <div class="span12">
        <a class="btn btn-success" id="btn_saveAndBack">保存</a>
        <a href="${request.contextPath}/product/skuDetail?productSkuId=${sku.productSkuId}&productId=${productId}" class="btn btn-info" id="btn_refresh">刷新</a>
        <a href="${request.contextPath}/product/infoDetail?productId=${productId}" class="btn btn-info" id="btn_back">返回商品信息</a>
        <div class="widget-box">
            <div class="widget-title"> <span class="icon"> <i class="icon-pencil"></i> </span>
                <h5>商品规格属性</h5>
            </div>
            <div class="widget-content nopadding">
                <form class="form-horizontal" method="post" action="${request.contextPath}/product/saveSKU" name="sku_form" id="sku_form">
                    <input type="hidden" name="productSkuId" value="${sku.productSkuId}" />
                    <input type="hidden" name="productId" value="${productId}" />
                    <input type="hidden" name="skuSpecs" value=""/>
                    <div class="control-group">
                        <label class="control-label">编号：</label>
                        <div class="controls">
                            <input type="text" name="skuCode" value="${sku.skuCode}">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">名称：</label>
                        <div class="controls">
                            <input type="text" name="skuName" value="${sku.skuName}">
                        </div>
                    </div>
                    <#list specList as spec>
                        <div class="control-group">
                            <label class="control-label">${spec.productSpec.specName}：</label>
                            <div class="controls">
                                <select>
                                    <option value="">--请选择--</option>
                                    <#list spec.productSpec.specValues?split(",") as specValue>
                                        <option value="${spec.productSpec.specName}：${specValue}">${specValue}</option>
                                    </#list>
                                </select>
                            </div>
                        </div>
                    </#list>
                    <div class="control-group">
                        <label class="control-label">成本价：</label>
                        <div class="controls">
                            <input type="text" name="purchasePrice" value="${sku.purchasePrice}">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">市场价：</label>
                        <div class="controls">
                            <input type="text" name="marketPrice" value="${sku.marketPrice}">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">销售价：</label>
                        <div class="controls">
                            <input type="text" name="sellPrice" value="${sku.sellPrice}">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="${request.contextPath}/js/jquery-1.12.2.min.js"></script>
<script src="${request.contextPath}/js/jquery.validate.js"></script>
<script src="${request.contextPath}/js/sweetalert.min.js"></script>
<script src="${request.contextPath}/js/select2.full.min.js"></script>
<script type="text/javascript">
    $(function(){
//        var goon = 0;   //是否继续添加, 0：返回，1：继续

        $("#btn_back").click(function(){
            loadPage($(this).attr("href"));
            return false;
        });

        $("#btn_refresh").click(function(){
            loadPage($(this).attr("href"));
            return false;
        });

//        $("#btn_save").click(function(){
//            goon = 1;
//            var skuSpec = $(":hidden[name='skuSpecs']");
//            $(":selected").each(function(i){
//                var spec = $(this).val();
//                if (spec.length > 0){
//                    if (i == 0){
//                        skuSpec.val(spec);
//                    }
//                    else{
//                        skuSpec.val(skuSpec.val() + "," + spec);
//                    }
//                }
//            });
//
//            $("#sku_form").submit();
//        });

        $("#btn_saveAndBack").click(function(){
//            goon = 0;
            var skuSpec = $(":hidden[name='skuSpecs']");
            $(":selected").each(function(i){
                var spec = $(this).val();
                if (spec.length > 0){
                    if (i == 0){
                        skuSpec.val(spec);
                    }
                    else{
                        skuSpec.val(skuSpec.val() + "," + spec);
                    }
                }
            });

            $("#sku_form").submit();
        });

        $("#sku_form").validate({
            submitHandler: function(form){
                $.ajax({
                    url: $(form).attr("action"),
                    type: "post",
                    data: $(form).serialize(),
                    success: function(result){
                        swal({
                            title: "信息提示",
                            text: result.status == "success" ? "恭喜，操作已完成！" : "很遗憾，操作失败了！",
                            type: result.status,
                            animation: "slide-from-top"
                        }, function(){
                            if (result.status == "success"){
//                                if (goon == 1){
//                                    loadPage("${request.contextPath}/product/skuDetail?productId=${productId}")
//                                }
//                                else{
                                    loadPage("${request.contextPath}/product/infoDetail?productId=${productId}");
//                                }
                            }
                        });
                    }
                });
            },
            rules: {
                skuName: {
                    "required": true
                },
                skuCode: {
                    "required": true
                }
            },
            errorClass: "help-inline",
            errorElement: "span",
            highlight:function(element, errorClass, validClass) {
                $(element).parents('.control-group').addClass('error');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).parents('.control-group').removeClass('error');
                $(element).parents('.control-group').addClass('success');
            }

        });
    })
</script>